package com.taffywen.study.spring_cloud_learn.demo;

import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


// 请确保您已将 API Key 存储在环境变量 ARK_API_KEY 中
// 初始化Ark客户端，从环境变量中读取您的API Key

/**
 * 对接豆包 api 的demo 示例
 */
public class ChatCompletionsVisionExample {
    // 从环境变量中获取您的 API Key。此为默认方式，您可根据需要进行修改
//    static String apiKey = System.getenv("ARK_API_KEY");
    static String apiKey = "9b4d99fb-c0cb-4889-8308-820fb2341ce2";
    // 此为默认路径，您可根据业务所在地域进行配置
    static String baseUrl = "https://ark.cn-beijing.volces.com/api/v3";
    static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
    static Dispatcher dispatcher = new Dispatcher();
    static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).baseUrl(baseUrl).apiKey(apiKey).build();

    public static void main(String[] args) {
        System.out.println("----- image input -----");
        final List<ChatMessage> messages = new ArrayList<>();
        final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
        multiParts.add(ChatCompletionContentPart.builder().type("image_url").imageUrl(
                new ChatCompletionContentPart.ChatCompletionContentPartImageURL(
                        "https://ts1.tc.mm.bing.net/th/id/R-C.a801c1ac08ba27b6fb9ca20df51a1aec?rik=ry6aja7ZNNPHKQ&riu=http%3a%2f%2fpic.kuaizhan.com%2fg3%2f43%2fe3%2fc9d2-9676-44a7-812a-5c10a4b4b3c987%2fimageView%2fv1%2fthumbnail%2f640x0&ehk=o8E3OvlUbr%2bqFqQ3pm2ssi7jAeeW6AoQGvkoye%2bf7kE%3d&risl=&pid=ImgRaw&r=0&sres=1&sresct=1"
                )
        ).build());
        multiParts.add(ChatCompletionContentPart.builder().type("text").text(
                "这是哪里？"
        ).build());

        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER)
                .multiContent(multiParts).build();
        messages.add(userMessage);

        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                // 指定您创建的方舟推理接入点 ID，此处已帮您修改为您的推理接入点 ID
                .model("doubao-seed-1-6-250615")
                .messages(messages)
                .build();
        Long startTime = System.currentTimeMillis();
        System.out.println("start time: "+startTime);
        service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));

        service.shutdownExecutor();
        System.out.println("end time: "+ (System.currentTimeMillis() - startTime) + " ms");
    }
}
